# -*-coning:utf-8-*-
from django.shortcuts import render
from django.views.generic.base import TemplateView
from django.contrib.auth import authenticate, login
from django.http.response import HttpResponse
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from .decorators import login_required


# Create your views here.

from rest_framework import mixins
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
from . import  zabbix
import json


class HostView(mixins.ListModelMixin, ViewSet):
    """
    主机列表，根据组来查询
    """

    def list(self, request, *args, **kwargs):
        group = request.query_params.get('group', None)
        if group:
            return Response(zabbix.host_list(group))
        else:
            return Response(zabbix.host_list())



class GetNetworkView(mixins.ListModelMixin, ViewSet):
    """
    获取网卡流量
    """
    def list(self, request, *args, **kwargs):
        hostid = request.query_params.get('hostid', None)
        flow_out = request.query_params.get('flow_out', None)
        flow_in = request.query_params.get('flow_in', None)
        card_in = request.query_params.get('card_in', None)
        card_out = request.query_params.get('card_out', None)
        return Response(zabbix.get_network(hostid, flow_out, flow_in, card_in, card_out))


class GetNewNetworkView(mixins.ListModelMixin, ViewSet):
    """
    获取最新网卡流量
    """
    def list(self, request, *args, **kwargs):
        hostid = request.query_params.get('hostid', None)
        flow_out = request.query_params.get('flow_out', None)
        flow_in = request.query_params.get('flow_in', None)
        card_in = request.query_params.get('card_in', None)
        card_out = request.query_params.get('card_out', None)
        return Response(zabbix.get_last_network(hostid, flow_out, flow_in, card_in, card_out))


# class UserLoginView(TemplateView):
#     template_name = "login.html"
#
#     def get(self, request, *args, **kwargs):
#         return render(request, self.template_name, {
#             "title": "用户登陆"
#         })
#
#     def post(self, request, *args, **kwargs):
#         # next_url = request.GET.get("next") if request.GET.get("next", None) else "/index"
#         next_url = request.session.get('next',reverse('api:index'))
#         data = request.POST.dict()
#         print(data)
#         user = authenticate(**data)
#         ret = {"status": 0, 'errmsg': ''}
#         if user is not None:
#             if user.is_active:
#                 login(request, user)
#                 ret['url'] = next_url
#             else:
#                 ret['status'] = 1
#                 ret['errmsg'] = "权限未开通，请发送邮件到op-it@ucfgroup.com申请开通"
#         else:
#             ret['status'] = 2
#             ret['errmsg'] = "用户名或密码错误"
#             # # 跳转到登录界面
#             # return redirect(reverse('api:login'))
#         return HttpResponse(json.dumps(ret), content_type="application/json")


def login_s(request):
    if request.method == "POST":
        username = request.POST.get('user', '')
        password = request.POST.get('pwd', '')
        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            login(request, user)
            request.session['islogin'] = True
            return redirect(reverse('api:index'))
        else:
            return render(request, 'login.html')
    else:
        return render(request, 'login.html')



@login_required
def index(request):
    '''显示登录页面'''
    return render(request,'init.html')


def return_404(request):
    return render(request,'404.html')


class Top5GroupView(mixins.ListModelMixin, ViewSet):
    """
    top5组 服务器数量
    """
    def list(self, request, *args, **kwargs):
        return Response(zabbix.get_top5_hosts())


class CPUView(mixins.ListModelMixin, ViewSet):
    """
    CPU一小时使用率
    """
    def list(self, request, *args, **kwargs):
        hostid = request.query_params.get('hostid', None)
        return Response(zabbix.cpu_list(hostid))


class CPUNewView(mixins.ListModelMixin, ViewSet):
    """
    CPU最新一分钟使用率
    """
    def list(self, request, *args, **kwargs):
        hostid = request.query_params.get('hostid', None)
        return Response(zabbix.cpu_new_list(hostid))


class TriggerView(mixins.ListModelMixin, ViewSet):
    """
    报警条数
    """
    def list(self, request, *args, **kwargs):
        return Response(zabbix.get_trigger_list())


class EventView(mixins.ListModelMixin, ViewSet):
    """
    TOP5event
    """
    def list(self, request, *args, **kwargs):
        return Response(zabbix.event_list())